01. Introduction to Operators and Expressions
Operators and Expressions Intro
Def and Example
To follow along with the code in this lesson open the Operators and Expressions playground in Playground Collection → Part 1 - Pirate Fleet → Operators and Expressions of the Beginning iOS Playground Collection. Play around with the examples and see what you can get Swift to do!
The first thing you need to know about operators and expressions are their simple definitions:
- Operators are special symbols or phrases that can be used to check, change, or combine values.
- Expressions are statements that can be reduced to a single value. In programming lingo, you’d say expressions are statements that return or “can be reduced to” a single value.
To really capture these concepts, let’s look at a fictitious calculation for an adventure game:
total points 1
This calculation rewards lives, penalizes deaths, and adds a random bonus.
Calc
This calculation reads that totalPoints are calculated by taking gamePoints, adding some points for the numberOfLives, removing some points for the numberOfDeaths, and then adding some random bonus. Let’s highlight the operators in this statement:
total points ops
Each operator in this calculation is highlighted in red.
operators
Most of these operators you’ve probably seen before in a math class—like addition (+), subtraction (-), and multiplication (*). We also have the equals sign (=) which is known as the assignment operator. Most of these operators work as you might expect, and we can really see this once we start substituting values into the calculation.
Operators in Action
Here is our calculation again, and we'll use it as an example:
total points again
In the upcoming examples, we'll use the same "totalPoints" calculation.
number of lives
Let’s assume that before we calculate this, we have 3 lives (numberOfLives), 0 deaths (numberOfDeaths), and 1000 gamePoints. Also, let’s assume the pointsPerLife is 100 and the pointsPerDeath is 300 (or -300 once we subtract it). So, we can substitute those values, and we get the following result:
more calcs
The initial values are 1000 game points, 3 lives, 100 points per life, 0 deaths, and 300 points per death.
Literal Expression
What are we seeing here? Well, each time we substitute a value for its corresponding variable name, we are seeing our first type of expression: a literal expression. For example, when the variable numberOfLives is reduced to its actual value of 3—that is a literal expression. We call it a literal expression because the resulting value (in this case 3) is a literal value. Remember a literal is just another word for a fixed value.
random bonus
First, the randomBonus is evaluated, and then the result is computed.
reducing
As we reduce the rest of this statement, the randomBonus is reduced just like the other expressions. Right now, we won't talk about the randomBonus expression—called a function call expression—but just assume it reduces to the value 20 for this example. And, as you can see, the result is 1320 and that will be assigned to totalPoints.